import pymssql, datetime
from Utils.gy import timeklpk


def Database():  # 获取前10客流名
    server = 'moonwife.top:9800'
    user = 'sa'
    password = 'ipva@07'
    database = 'IPVA_S0400_B1'
    kldb = pymssql.connect(server, user, password, database)
    cursor = kldb.cursor()
    return cursor


def weekly():  # 获取上周同日此时客流
    cu = Database()
    now = datetime.date.today()
    oneday = datetime.timedelta(days=7)
    nowtime = (datetime.datetime.now() + datetime.timedelta(minutes=-10)).strftime('%H:%M:')
    now1 = '\'' + str(now - oneday) + ' 00:00:00.000' + '\''
    now2 = '\'' + str(now - oneday) + ' ' + str(nowtime) + '00.000' + '\''
    sql = '''
    select InSum
    from IPVA_S0400_B1.dbo.Summary_Ten_Noues
    where
    SiteKey='P00021'
    and
    CountDate>=''' + now1 + '''
    and
    CountDate<''' + now2 + '''
    ;
    '''
    cu.execute(sql)
    daytimekl = cu.fetchall()
    a = []
    for i in daytimekl:
        nubs = i[0]
        a.append(nubs)
    nub = sum(a)
    cu.close()
    return nub


def get_klPercentage():  # 上周同日时段客流对比结果
    firstkl = int(weekly())
    thisday = int(timeklpk.get_kldata()[0])
    Percentage = float((thisday - firstkl) / firstkl * 100)
    Percentage = float('%.2f' % Percentage)
    Percentage = str(Percentage)
    return Percentage
